package com.jike.jkgame;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.jike.jkgame.data.AttackPreviewData;
import com.jike.jkgame.data.Player;

public class AttackPreviewDialog extends DialogFragment implements
		OnClickListener {

	private AttackPreviewData mData;
	private OnActionListener mListener;

	public static AttackPreviewDialog newInstance(OnActionListener listner, AttackPreviewData data) {
		AttackPreviewDialog dialog = new AttackPreviewDialog(listner);

		Bundle args = new Bundle();
		args.putSerializable("data", data);
		dialog.setArguments(args);

		return dialog;
	}
	
	public AttackPreviewDialog(OnActionListener listner) {
		super();
		mListener = listner;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		getDialog().requestWindowFeature(STYLE_NO_TITLE);
		mData = (AttackPreviewData) getArguments().getSerializable("data");
		View v = inflater.inflate(R.layout.attackpreviewdialog_layout,
				container, false);
		v.findViewById(R.id.btn_close).setOnClickListener(this);
		v.findViewById(R.id.btn_attack).setOnClickListener(this);
		v.findViewById(R.id.btn_cancel).setOnClickListener(this);
		
		Rect displayRectangle = new Rect();
		Window window = getDialog().getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
		v.setMinimumWidth((int) (displayRectangle.width() * 0.7f));
		v.setMinimumHeight((int) (displayRectangle.height() * 0.9f));

		if (mData != null) {
			TextView tv = (TextView) v.findViewById(R.id.tv_win);
			tv.setText(String.format(Utilis
					.getResString(R.string.attack_preview_win_format),
					mData.winExp, mData.winMoney));

			tv = (TextView) v.findViewById(R.id.tv_fail);
			tv.setText(String.format(Utilis
					.getResString(R.string.attack_preview_fail_format),
					mData.failExp, mData.failMoney));

			tv = (TextView) v.findViewById(R.id.tv_username1);
			tv.setText(mData.defense.rname);
		}

		return v;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_attack:
			mListener.doAttack(mData.defense);
			getDialog().dismiss();
			break;
		case R.id.btn_cancel:
		case R.id.btn_close:
			mListener.cancel(mData.defense);
			getDialog().dismiss();
			break;
		default:
			break;
		}
	}

	public interface OnActionListener {
		public void doAttack(Player player);
		public void cancel(Player player);
	}
	
}
